package com.hd.trans.ui.adapter.vm

import android.graphics.Color
import android.graphics.Typeface
import android.view.View
import android.view.ViewGroup.MarginLayoutParams
import com.biubiu.universaladapter.base.vm.DbViewModule
import com.hd.trans.R
import com.hd.trans.databinding.VmTransSubsectionBinding
import com.hd.trans.network.bean.TransSubsection
import com.hd.trans.utils.ScreenUtil

enum class Mode {
    MODE_MERGE, MODE_COLUMN_TOP, MODE_COLUMN_BOTTOM
}

class TopSubsectionVm : SubsectionVm(Mode.MODE_COLUMN_TOP)
class BottomSubsectionVm : SubsectionVm(Mode.MODE_COLUMN_BOTTOM)
class MergeSubsectionVm : SubsectionVm(Mode.MODE_MERGE)

open class SubsectionVm(private var mode: Mode) : DbViewModule<TransSubsection, VmTransSubsectionBinding>(R.layout.vm_trans_subsection) {

    private val textColorDark = Color.parseColor("#16181A")
    private val textColorBlue = Color.parseColor("#3080F9")
    private val textColorLight = Color.WHITE

    private val highlightStateMap = hashMapOf<Int,Boolean>()

    var onContentDisplay:((hasContent:Boolean)->Unit)? = null

    private var mHasContent = false

    override fun onBindData(dataBinding: VmTransSubsectionBinding, data: TransSubsection, dataPosition: Int, layoutPosition: Int, payloads: List<*>) {
        onBindData(dataBinding,data,dataPosition,layoutPosition)
    }

    override fun onBindData(dataBinding: VmTransSubsectionBinding, data: TransSubsection, dataPosition: Int, layoutPosition: Int) {
        var originalContent = data.originalContent.trim()
        data.msg?.let {
            originalContent += it
        }
        var translateContent = data.translateContent.trim()
        data.msg2?.let {
            translateContent += it
        }
        val transFontSize = SimuTextSize.loadRecord()

        dataBinding.tvOriginal.text = originalContent
        dataBinding.tvTranslate.text = translateContent
        var hasContent = false

        when(mode){
            Mode.MODE_MERGE -> {
                dataBinding.tvOriginal.visibility = View.VISIBLE
                dataBinding.tvTranslate.visibility = View.VISIBLE

                hasContent = originalContent.isNotEmpty() || translateContent.isNotEmpty()

                dataBinding.tvOriginal.setTextColor(textColorDark)
                dataBinding.tvOriginal.setTypeface(null,Typeface.NORMAL)
                dataBinding.tvTranslate.setTextColor(textColorBlue)
                dataBinding.tvTranslate.setTypeface(null,Typeface.BOLD)

                SimuTextSize.applyTextSize(dataBinding.tvOriginal,transFontSize)
                SimuTextSize.applyTextSize(dataBinding.tvTranslate,transFontSize)

                var nextIsEmpty = true
                if (dataPosition < size()-1){
                    val nextData = dataList[dataPosition+1]
                    nextIsEmpty = nextData.isEmpty()
                }

                //如果下一个item为空则设置高亮
                dataBinding.tvOriginal.alpha = if (nextIsEmpty) 1f else 0.6f
                dataBinding.tvTranslate.alpha = if (nextIsEmpty) 1f else 0.6f
                highlightStateMap[dataPosition] = nextIsEmpty

                (dataBinding.tvOriginal.layoutParams as MarginLayoutParams).bottomMargin = ScreenUtil.dp2px(0f)
                (dataBinding.tvTranslate.layoutParams as MarginLayoutParams).bottomMargin = ScreenUtil.dp2px(20f)

                val isEmpty = originalContent.isEmpty()
                if (isEmpty){
                    dataBinding.viewPoint.visibility = View.INVISIBLE
                }
                else{
                    dataBinding.viewPoint.visibility = View.VISIBLE
                }

            }
            Mode.MODE_COLUMN_TOP -> {
                dataBinding.viewPoint.visibility = View.INVISIBLE

                dataBinding.tvOriginal.setTextColor(textColorLight)
                dataBinding.tvOriginal.setTypeface(null,Typeface.NORMAL)
                dataBinding.tvTranslate.setTextColor(textColorLight)
                dataBinding.tvTranslate.setTypeface(null,Typeface.NORMAL)

                SimuTextSize.applyTextSize(dataBinding.tvOriginal,transFontSize)
                SimuTextSize.applyTextSize(dataBinding.tvTranslate,transFontSize)

                (dataBinding.tvOriginal.layoutParams as MarginLayoutParams).bottomMargin = ScreenUtil.dp2px(10f)
                (dataBinding.tvTranslate.layoutParams as MarginLayoutParams).bottomMargin = ScreenUtil.dp2px(10f)

                if (data.isFromLang){
                    dataBinding.tvOriginal.visibility = View.VISIBLE
                    dataBinding.tvTranslate.visibility = View.GONE

                    hasContent = originalContent.isNotEmpty()
                }
                else{
                    dataBinding.tvOriginal.visibility = View.GONE
                    dataBinding.tvTranslate.visibility = View.VISIBLE

                    hasContent = translateContent.isNotEmpty()
                }

            }
            Mode.MODE_COLUMN_BOTTOM -> {
                dataBinding.viewPoint.visibility = View.INVISIBLE

                dataBinding.tvOriginal.setTextColor(textColorDark)
                dataBinding.tvOriginal.setTypeface(null,Typeface.NORMAL)
                dataBinding.tvTranslate.setTextColor(textColorDark)
                dataBinding.tvTranslate.setTypeface(null,Typeface.NORMAL)

                SimuTextSize.applyTextSize(dataBinding.tvOriginal,transFontSize)
                SimuTextSize.applyTextSize(dataBinding.tvTranslate,transFontSize)

                (dataBinding.tvOriginal.layoutParams as MarginLayoutParams).bottomMargin = ScreenUtil.dp2px(10f)
                (dataBinding.tvTranslate.layoutParams as MarginLayoutParams).bottomMargin = ScreenUtil.dp2px(10f)

                if (data.isFromLang){
                    dataBinding.tvOriginal.visibility = View.GONE
                    dataBinding.tvTranslate.visibility = View.VISIBLE

                    hasContent = translateContent.isNotEmpty()
                }
                else{
                    dataBinding.tvOriginal.visibility = View.VISIBLE
                    dataBinding.tvTranslate.visibility = View.GONE

                    hasContent = originalContent.isNotEmpty()
                }

            }
        }

        if (hasContent){
            mHasContent = true
        }

        onContentDisplay?.invoke(hasContent or mHasContent)

    }

    override fun updateList(list: List<TransSubsection>?, payload: Any) {
        mHasContent = false
        super.updateList(list,payload)
    }

    fun getAllContent(): String {
        return when(mode){
            Mode.MODE_MERGE -> {
                val builder = StringBuilder()
                dataList.forEach {
                    builder.append(it.originalContent)
                        .append("\n")
                        .append(it.translateContent)
                        .append("\n")
                }
                builder.toString().trim()
            }
            Mode.MODE_COLUMN_TOP -> {
                val builder = StringBuilder()
                dataList.forEach {
                    if (it.isFromLang){
                        builder.append(it.originalContent)
                            .append("\n")
                    }
                    else{
                        builder.append(it.translateContent)
                            .append("\n")
                    }
                }
                builder.toString().trim()
            }
            Mode.MODE_COLUMN_BOTTOM -> {
                val builder = StringBuilder()
                dataList.forEach {
                    if (it.isFromLang){
                        builder.append(it.translateContent)
                            .append("\n")
                    }
                    else{
                        builder.append(it.originalContent)
                            .append("\n")
                    }
                }
                builder.toString().trim()
            }
        }
    }

    fun updateSubsection(subsection: TransSubsection){
        val index = dataList.indexOf(subsection)
        if (index == -1)
            return
        notifyItemChanged(index,"updateSubsection")
    }

    fun updateTextSize(){
        for (i in dataList.indices){
            notifyItemChanged(i,"updateTextSize")
        }
    }

    fun updateHighlightState(){
        val list = dataList
        val dataCount = list.size
        for (i in list.indices){
            var nextIsEmpty = true
            if (i < dataCount-1){
                val nextData = list[i+1]
                nextIsEmpty = nextData.isEmpty()
            }
            if (highlightStateMap[i] != nextIsEmpty){
                updateSubsection(list[i])
            }
        }
    }

    override fun isNotEmpty():Boolean{
        val list = dataList
        if (list.isEmpty())
            return false
        val lastData = list[list.size-1]
        return when(mode){
            Mode.MODE_COLUMN_TOP -> {
                if (lastData.isFromLang){
                    lastData.hasOriginalContent() || !lastData.msg.isNullOrEmpty()
                }
                else{
                    lastData.hasTranslateContent()
                }
            }
            Mode.MODE_COLUMN_BOTTOM -> {
                if (lastData.isFromLang){
                    lastData.hasTranslateContent()
                }
                else{
                    lastData.hasOriginalContent() || !lastData.msg.isNullOrEmpty()
                }
            }
            else -> {
                !lastData.isEmpty()
            }
        }
    }

}